package io.gbznt.modules.buss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.gbznt.common.utils.Constant;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.modules.buss.constatnt.MaintainTaskSourceTypeEnum;
import io.gbznt.modules.buss.entity.*;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.buss.service.BussRtuEquipmentService;
import io.gbznt.modules.buss.service.BussRtuInfoService;
import io.gbznt.modules.buss.service.BussRtuWarningRecordService;
import io.gbznt.modules.sys.entity.SysDeptEntity;
import io.gbznt.modules.sys.entity.SysDictEntity;
import io.gbznt.modules.sys.entity.SysUserEntity;
import io.gbznt.modules.sys.service.SysDeptService;
import io.gbznt.modules.sys.service.SysDictService;
import io.gbznt.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.Query;

import io.gbznt.modules.buss.dao.BussMaintainTaskDao;
import io.gbznt.modules.buss.service.BussMaintainTaskService;

@Service("bussMaintainTaskService")
public class BussMaintainTaskServiceImpl extends ServiceImpl<BussMaintainTaskDao, BussMaintainTaskEntity>
		implements BussMaintainTaskService {

	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private BussMaintainTaskService bussMaintainTaskService;
	@Autowired
	private BussFarmlandInfoService bussFarmlandInfoService;
	@Autowired
	private BussRtuEquipmentService bussRtuEquipmentService;
	@Autowired
	private BussRtuWarningRecordService bussRtuWarningRecordService;
	@Autowired
	private BussRtuInfoService bussRtuInfoService;
	@Autowired
	private SysDictService sysDictService;
	@Autowired
	private SysDeptService sysDeptService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String businessNum = (String) params.get("businessNum");
		// 田块id
		String farmlandId = (String) params.get("farmlandId");
		String status = (String) params.get("status");
		String isMe = (String) params.get("isMe");

		IPage<BussMaintainTaskEntity> page = this.page(new Query<BussMaintainTaskEntity>().getPage(params),
				new QueryWrapper<BussMaintainTaskEntity>()
						.like(StringUtils.isNotEmpty(businessNum), "business_num", businessNum)
						.eq(StringUtils.isNotEmpty(farmlandId), "farmland_info_id", farmlandId)
						.eq(StringUtils.isNotEmpty(status), "status", status)
						.apply(StringUtils.isNotEmpty(isMe), "(create_id = {0} or handler = {0}) and status in (0,1)",
								ShiroUtils.getUserId(), ShiroUtils.getUserId())
						// 个人数据权限控制
						.in(!Constant.SUPER_ADMIN.equals(ShiroUtils.getUserId()),
								"dept_id", sysDeptService.getPermissionDeptIdsArr(true,true))
						.orderByDesc("create_time")
		);

		for (BussMaintainTaskEntity bussMaintainTaskEntity : page.getRecords()) {

			// 负责人信息
			SysUserEntity sysUserEntity = sysUserService.getById(bussMaintainTaskEntity.getHandler());
			if (null != sysUserEntity) {
				bussMaintainTaskEntity.setHandlerName(sysUserEntity.getName());
			}

			// 部门信息
			SysDeptEntity deptEntity = sysDeptService.getById(bussMaintainTaskEntity.getDeptId());
			if(null != deptEntity){
				bussMaintainTaskEntity.setDeptName(deptEntity.getName());
			}

			// 填入设备信息
			BussRtuEquipmentEntity bussRtuEquipmentEntity = bussRtuEquipmentService
					.getById(bussMaintainTaskEntity.getEquipmentId());
			if (null != bussRtuEquipmentEntity) {
				bussMaintainTaskEntity.setEquipmentName(bussRtuEquipmentEntity.getName());

				BussRtuInfoEntity rtuInfoEntity = bussRtuInfoService.getById(
						bussRtuEquipmentEntity.getRtuId());
				if(null!= rtuInfoEntity){
					bussMaintainTaskEntity.setRtuSerialNum(rtuInfoEntity.getSerialNum());
					BussFarmlandInfoEntity farmlandInfoEntity = bussFarmlandInfoService.getById(
							rtuInfoEntity.getFarmlandInfoId());
					if(null != farmlandInfoEntity){
						bussMaintainTaskEntity.setRtuFarmlandName(farmlandInfoEntity.getName());
					}
				}

			}

			// 告警生成的记录填充告警内容
			if (bussMaintainTaskEntity.getSourceType() == MaintainTaskSourceTypeEnum.WARNING.getValue()) {
				BussRtuWarningRecordEntity bussRtuWarningRecordEntity = bussRtuWarningRecordService
						.getById(bussMaintainTaskEntity.getWarningRecordId());
				if (null != bussRtuWarningRecordEntity) {
					bussMaintainTaskEntity.setWarningRecordText(bussRtuWarningRecordEntity.getWarningText());
				}

			}

			// 紧急程度（字典）
			if (StringUtils.isNotBlank(bussMaintainTaskEntity.getGradeNum())) {
				SysDictEntity dict = sysDictService.getById(bussMaintainTaskEntity.getGradeNum());
				if (dict != null && StringUtils.isNotBlank(dict.getValue())) {
					bussMaintainTaskEntity.setGradeNumName(dict.getValue());
				}
			}
		}

		return new PageUtils(page);
	}

}
